/*
 * Copyright (C) 2016 The Android Open Source Project
 *  Created by  Ant  2016/9/30  11:18
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.ant.birds.views;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;

import com.ant.birds.R;
import com.ant.birds.beans.BirdBean;
import com.ant.birds.beans.FloorBean;
import com.ant.birds.utils.BitmapUtils;
import com.ant.utils.utils.AntLog;
import com.ant.utils.views.BaseSurfaceView;

/**
 * Created by baichuan on 2016/9/30.
 */
public class GamePanel extends BaseSurfaceView {
    private Bitmap panelBg;
    private BirdBean bired;
    private FloorBean floor;


    public GamePanel(Context context) {
        super(context);
        initView();
    }

    public GamePanel(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public GamePanel(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }


    private void initView() {
        bired = new BirdBean(getResources(), R.drawable.b1);

        panelBg = BitmapFactory.decodeResource(getResources(), R.drawable.bg1);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        panelBg = BitmapUtils.getBitmap(panelBg, getWidth(), getHeight());
        floor = new FloorBean(getResources(), w, h / 4 - 40);
        bired.setX(w / 4);
        bired.setY(h / 3);
        bired.setMinLow(h - h / 4);
        bired.setSlowSpeed(15);

        floor.setX(0);
        floor.setY(h - h / 4 + 40);
    }


    @Override
    public void draws(Canvas canvas) {
        super.draw(canvas);
        canvas.drawBitmap(panelBg, 0, 0, null);
        //AntLog.i("drawing ant");
        floor.drawMe(canvas);
        bired.drawSelf(canvas);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_UP:
                AntLog.i("down=dd");
                bired.setBiredUp(100);
                break;
        }
        return true;
    }
}
